<?php


class MapController extends Zend_Controller_Action 
{
	public function init()
	{
		$baseurl = $this->getFrontController()->getBaseUrl();
		$this->view->headScript()->appendFile( "http://maps.googleapis.com/maps/api/js?sensor=false", $type = 'text/javascript' );
		$this->view->headScript()->appendFile( $baseurl."/gmap3/gmap3.min.js", $type = 'text/javascript' );
		
	}
	
	public function indexAction()
	{
		$id_address = $this->_request->getParam("id_address");
		$addressMapper = new Application_Model_AddressMapper();
		$address = $addressMapper->find($id_address, new Application_Model_Address());
		$script = "$(function(){
	
								$('.gmap3').gmap3({
									marker:{
									     	 	latLng: [".$address->getLatitude().", ".$address->getLongitude()."]
									    	},
									map:{
		
								      options:{
								        zoom:14,
								        mapTypeId: google.maps.MapTypeId.ROADMAP,
								        mapTypeControl: true,
								        mapTypeControlOptions: {
								          style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
								        },
								        navigationControl: false,
								        scrollwheel: false,
								        streetViewControl: false,
										zoomControlOptions: {
												style: google.maps.ZoomControlStyle.SMALL
												},
										panControl: false,
								      }
								    }
								});
	
					})";
	
		$this->view->headScript()->appendScript($script, $type = 'text/javascript');
	
	}
	
	public function addresspickerAction()
	{
		$id_address = $this->_request->getParam("id_address");
		$addressMapper = new Application_Model_AddressMapper();
		$address = $addressMapper->find($id_address, new Application_Model_Address());
		$script = "$(function(){
								var clicked = false;
								$('.dropdown-map').click(function(){
												setTimeout(function(){
												  	$('.gmap3').gmap3({trigger:'resize'});
													if(!clicked){
													$('.gmap3')
														.gmap3({
															map:{
																	options:{
																		center:[46.578498,2.457275]
																	}
																}
														});
												}
												clicked = true;
												}, 100);
												  
												});
								$('.gmap3').gmap3({
								  	marker:{
								    latLng: [46.578498,2.457275],
								    options:{
								      draggable:true
								    },
								    events:{
									  dragstart: function(){
												         $(this).gmap3({clear:'overlay'});
												      },
								      dragend: function(marker){
								        $(this).gmap3({
								          getaddress:{
								            latLng:marker.getPosition(),
								            callback:function(results){
								              var map = $(this).gmap3('get'),
								                infowindow = $(this).gmap3({get:'infowindow'});
								                //content = results && results[1] ? results && results[1].formatted_address : 'no address';
												if(results && results[1]){
													var elt = results[0].address_components;
													//alert(results[0].address_components);
											          for(i in elt){
											            if(elt[i].types[0] == 'postal_code')
															$('#zipCode').val(elt[i].short_name);
											            if(elt[i].types[0] == 'locality')
															$('#city').val(elt[i].long_name);
											            if(elt[i].types[0] == 'administrative_area_level_2'){
															$('#admin_area_lvl2').val(elt[i].long_name);
															admin_area_lvl2 = elt[i].long_name;
														}
														if(elt[i].types[0] == 'administrative_area_level_1'){
															$('#admin_area_lvl1').val(elt[i].long_name);
															admin_area_lvl1 = elt[i].long_name;
														}
											            if(elt[i].types[0] == 'country')
															$('#country').val(elt[i].long_name);
														
											          }
													lat = results[0].geometry.location.lat();
													lng = results[0].geometry.location.lng();
													$('#latitude').val(lat);
													$('#longitude').val(lng);
													content = admin_area_lvl2 + ', ' + admin_area_lvl1;
												}else{
													content = 'no address';
												}
								              if (infowindow){
								                infowindow.open(map, marker);
								                infowindow.setContent(content);
								              } else {
								                $(this).gmap3({clear:'overlay'},
													          {
													          overlay:{
													            latLng: marker.getPosition(),
													            options:{
																	content : '<div class=\"mapTooltip\">' + content + '</div>',
																	offset: {
															                x:15,
															                y:-40
															        }
													            }
													          }
													      });
								              }
								            }
								          }
								        });
								      }
								    }
								  },
								  map:{
								    options:{
								      	zoom: 6,
										mapTypeId: google.maps.MapTypeId.ROADMAP,
								        mapTypeControl: true,
								        mapTypeControlOptions: {
								          style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
								        },
								        navigationControl: false,
								        scrollwheel: true,
								        streetViewControl: false,
										zoomControlOptions: {
												style: google.maps.ZoomControlStyle.SMALL
												},
										panControl: false,
								    }
								  }
								});
						
					})";
		//Solution
// 		$('.gmap3')
// 		.gmap3({trigger:'resize'});
// 		$('.gmap3')
// 		.gmap3({map:{
// 			options:{
// 				center:[46.578498,2.457275]
// 			}}});
		$this->view->headScript()->appendScript($script, $type = 'text/javascript');
		
	}
	
	public function reqmapAction()
	{
		//$id_address = $this->_request->getParam("id_address");
		$modules = $this->_request->getParams();
		
		$bloc = "";
		$i = 0;
		$addresses = $modules['addresses'];
		foreach ($addresses as $address){
			$bloc = $bloc."{latLng:[".$address->latitude.", ".$address->longitude."], data:{lat: '".$address->latitude."', lng: '".$address->longitude."', city: '".$address->city."', index: ".$i."}},\n";
			$i++;
		}
		
		$class = $modules['class'];
		
		$script = "
				function selectCity(index, updateAccordion) {
					//replace div by .thumbnail_grey------------------------------------------------------
					$( '#resultsContainer .thumbnail_grey' ).attr('class', 'thumbnail' );
					$( '#accordion-map-'+index ).attr( 'class', 'thumbnail_grey' );
					//$( '#resultsContainer' ).scrollTop($('#accordion-map-'+index).position());
					$('.gmap3').gmap3({
						
						exec: {
							name: 'marker',
							all:'true',
							func: function(value){
								
								//alert(index);
								// data.object is the google.maps.Marker object
								if (value.data.index == index) {
									value.object.setIcon('https://sites.google.com/site/tradenetworkkmldep/images/049bdb/".strtolower($class).".png');
									$(this).gmap3({clear:'overlay'},
													          {
													          overlay:{
													            latLng: value.object.getPosition(),
													            options:{
																	content : '<div class=\"mapTooltip\">' + value.data.city + '</div>',
																	offset: {
															                x:15,
															                y:-40
															        }
													            }
													          }
													      }
// Pour centrer le marker dans la map
,
{map:{
				options:{
					center:[value.data.lat,value.data.lng]
				}
			}
}
											); 
								} else {
									value.object.setIcon('https://sites.google.com/site/tradenetworkkmldep/images/aae1f7/".strtolower($class).".png');
								}
							}
						}
					});
				}
				
				$(function(){
					
					$( '#resultsContainer div' ).click(function() {
						selectCity($(this).attr('tabindex'));
					});
					$('.gmap3').gmap3({
								marker:{
										values:[
									      ".$bloc."
									      		],
									    options:{
									      		icon: 'https://sites.google.com/site/tradenetworkkmldep/images/aae1f7/".strtolower($class).".png',
									      		},
									    events:{
												      mouseover: function(marker, event, context){
												        var map = $(this).gmap3('get'),
												          infowindow = $(this).gmap3({get:{name:'infowindow'}});
												        if (infowindow){
												          infowindow.open(map, marker);
												          infowindow.setContent(context.data.city);
												        } else {
												          $(this).gmap3({clear:'overlay'},
													          {
													          overlay:{
													            latLng: marker.getPosition(),
													            options:{
																	content : '<div class=\"mapTooltip\">' + context.data.city + '</div>',
																	offset: {
															                x:15,
															                y:-40
															        }
													            }
													          }
													      }); 
												        }
												      },
												      mouseout: function(){
												         $(this).gmap3({clear:'overlay'});
												      },
									      			 click: function (marker, event, context) {
														selectCity(context.data.index, true);
													 }
											    }
									    },
								map:{
	
								      options:{
								        zoom:10,
								        mapTypeId: google.maps.MapTypeId.ROADMAP,
								        mapTypeControl: true,
								        mapTypeControlOptions: {
								          style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
								        },
								        navigationControl: false,
								        scrollwheel: false,
								        streetViewControl: false,
										zoomControlOptions: {
												style: google.maps.ZoomControlStyle.SMALL
												},
										panControl: false,
								      }
				    				}
								});
	
					
					})";
	
		$this->view->headScript()->appendScript($script, $type = 'text/javascript');
	
	}
	
	
}

?>